package com.wzbc.video.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzbc.video.entity.Chapter;
import com.wzbc.video.mapper.ChapterMapper;
import com.wzbc.video.req.ChapterDownReq;
import com.wzbc.video.req.ChapterReq;
import com.wzbc.video.req.IdReq;
import com.wzbc.video.req.SearchReq;
import com.wzbc.video.res.ChapterFileRes;
import com.wzbc.video.service.ChapterService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Override
    public List<Chapter> getChapter(ChapterReq req) {
        return this.baseMapper.selectList(new LambdaQueryWrapper<Chapter>()
                .eq(Chapter::getLessonId, req.getLessonId())
                .eq(req.getChapterAfterId() != null || req.getChapterAfterId() != 0, Chapter::getChapterAfterId, req.getChapterAfterId()));
    }

    @Override
    public List<Chapter> getChapterDown(ChapterDownReq req) {
        return this.baseMapper.selectList(new LambdaQueryWrapper<Chapter>()
                .eq(Chapter::getLessonId, req.getLessonId())
                .eq(Chapter::getChapterLevel, req.getChapterLevel()));
    }

    @Override
    public ChapterFileRes getChapterOne(Long id) {
        return this.baseMapper.getChapter(new QueryWrapper<ChapterFileRes>().eq("chapter.chapter_id", id)).get(0);
    }

    @Override
    public Integer deleteById(Long chapterId) {
        return this.baseMapper.deleteById(chapterId);
    }

    @Override
    public Integer insert(Chapter chapter) {
        Chapter chapterAfter = this.baseMapper.selectById(chapter.getChapterAfterId());
        if (chapterAfter != null) {
            if (chapterAfter.getChapterLevel() >= 3) {
                return 0;
            }
        }
        return this.baseMapper.insert(chapter);
    }
}
